﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public static class LoaiDiDAO
    {
        public static List<LoaiDiDTO> LoadAll()
        {
            List<LoaiDiDTO> result = new List<LoaiDiDTO>();
            SqlConnection connection = new SqlConnection(DataProvider.ConnectionSting);
            try
            {
                SqlCommand command = new SqlCommand("sp_SelectAllLoaiDi", connection);
                command.CommandType = CommandType.StoredProcedure;
                connection.Open();

                SqlDataReader reader;
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    LoaiDiDTO tmp = new LoaiDiDTO();
                    tmp.ID = reader.GetInt32(0);
                    tmp.Ten = reader.GetString(1);
                    result.Add(tmp);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return result;
        }

        public static bool Add(LoaiDiDTO newLoaiDi)
        {
            bool result = false;
            SqlConnection connection = new SqlConnection(DataProvider.ConnectionSting);
            try
            {
                SqlCommand command = new SqlCommand("sp_InsertLoaiDi", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ten", SqlDbType.NVarChar).Value = newLoaiDi.Ten;
                connection.Open();
                if (command.ExecuteNonQuery() != 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return result;

        }

        public static bool Alter(LoaiDiDTO newLoaiDi)
        {
            bool result = false;
            SqlConnection connection = new SqlConnection(DataProvider.ConnectionSting);
            try
            {
                SqlCommand command = new SqlCommand("sp_UpdateLoaiDi", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ten", SqlDbType.NVarChar).Value = newLoaiDi.Ten;
                command.Parameters.Add("@id", SqlDbType.Int).Value = newLoaiDi.ID;

                connection.Open();
                if (command.ExecuteNonQuery() != 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return result;

        }
    }
}
